package com.gzhu.knowledgeAdmin.controller;


import com.gzhu.knowledgeAdmin.common.result.R;
import com.gzhu.knowledgeAdmin.entity.TRwT;
import com.gzhu.knowledgeAdmin.repository.TRwTRepository;
import com.gzhu.knowledgeAdmin.service.TRwTService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/task")
public class TaskController {

    @Autowired
    private TRwTService tRwTService;

    @Resource
    private TRwTRepository tRwTRepository;

    @PostMapping("/del/{fRwid}")
    public R deleteById(@PathVariable Long fRwid)
    {
        return R.ok(tRwTService.removeById(fRwid));
    }

    @PostMapping("/find")
    public R find(@RequestBody TRwT tRwT)
    {
        return R.ok(tRwTService.selectBySearch(tRwT));
    }

    @GetMapping("/findById/{fRwid}")
    public R findById(@PathVariable Long fRwid)
    {
        return R.ok(tRwTService.findById(fRwid));
    }


    @PostMapping("/save")
    public R saveTask(@RequestBody TRwT tRwT)
    {
        Long id = tRwT.getFRwid();
        if(id==null)
        {
            return R.ok(tRwTService.saveTask(tRwT));
        }else {
            return R.ok(tRwTService.updateTask(tRwT));
        }
    }

    @GetMapping("/findOwner")
    public R findOwner()
    {
        return R.ok(tRwTService.findOwners());
    }



    @PostMapping("/start")
    public R startTasks(@RequestBody Map<String, List<Long>> ids) {
        List<Long> rwids = ids.get("rwids");
        return R.ok(tRwTService.startTasks(rwids));
    }


    @PostMapping("/stop")
    public R stopTasks(@RequestBody Map<String, List<Long>> ids) {
        List<Long> rwids = ids.get("rwids");
        return R.ok(tRwTService.stopTasks(rwids));
    }

    @PostMapping("/recall")
    public R recallTasks(@RequestBody Map<String, List<Long>> ids) {
        List<Long> rwids = ids.get("rwids");
        return R.ok(tRwTService.recallTasks(rwids));
    }

    @GetMapping("/allTasks")
    public R getAllTasks() {
        return R.ok(tRwTService.getAllTasks());
    }

    @GetMapping("/allTasksWithCompanyName")
    public R getAllTasksWithCompanyName() {
        return R.ok(tRwTService.getAllTasksWithCompanyName());
    }
}
